package com.ticket;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
import java.util.Scanner;

public class Password {
    public static void findPassword() {
        // 加载数据库配置
        InputStream resourceAsStream = Password.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        try {
            properties.load(resourceAsStream);
            String driverClass = properties.getProperty("driverClass");
            String url = properties.getProperty("url");
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            Class.forName(driverClass);
            Connection connection = DriverManager.getConnection(url, user, password);
            // SQL 查询语句
            String sql = "SELECT password FROM user WHERE username = ? AND realname = ? AND phone = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            Scanner scanner = new Scanner(System.in);
            while (true) {
                System.out.println("通过实名和手机号找回密码!(0.返回上一级菜单)");

                System.out.println("请输入用户名：");
                String username = scanner.nextLine();
                if (username.equals("0")) {
                    HomePage.home(); // 允许用户中止操作
                    break;
                }

                System.out.println("请输入真实姓名：");
                String realname = scanner.nextLine();
                if (realname.equals("0")) {
                    HomePage.home(); // 允许用户中止操作
                    break;
                }

                System.out.println("请输入手机号：");
                String phone = scanner.nextLine();
                if (phone.equals("0")) {
                    HomePage.home(); // 允许用户中止操作
                    break;
                }
                // 设置查询参数
                preparedStatement.setString(1, username);
                preparedStatement.setString(2, realname);
                preparedStatement.setString(3, phone);
                // 执行查询
                ResultSet resultSet = preparedStatement.executeQuery();
                if (resultSet.next()) {
                    // 用户名、真实姓名和手机号都匹配，输出密码
                    String userPassword = resultSet.getString("password");
                    System.out.println("您的密码是：" + userPassword);

                    String n = null;
                    while (true) {
                        System.out.println("1.继续操作\t2.返回登录界面");
                        n = scanner.next();
                        if (n.equals("1")) {
                            findPassword();
                            break;
                        } else if (n.equals("2")) {
                            HomePage.home();
                            break;
                        } else {
                            System.out.println("输入有误!");
                        }
                    }
                    if (n.equals("2"))
                        break;
                } else {
                    // 任一输入不匹配，提示用户重新输入
                    System.out.println("输入信息错误，请重新填写");
                }
                // 清理资源
                resultSet.close();
            }
            // 关闭连接和语句
            preparedStatement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}